<?php

namespace App\Repositories;

use App\Models\MoneyLogModel;
use App\Models\OperateModel;

class MoneyLogRepository extends BaseRepository
{
    /**
     * @param $params
     * @return array[]
     */
    public function getFilter ($params) {
        $user = request()->user();
        $where = [];
        $whereBetween = [];
        $where[] = [
            'admin_id', $user['id']
        ];
        if ($params['type']) {
            $where[] = [
                'type', $params['type']
            ];
        }
        if ($params['money']) {
            $where[] = [
                'money', $params['money']
            ];
        }
        if ($params['published_at']) {
            $whereBetween[] = [
                'published_at', $params['published_at']
            ];
        }
        return [
            'where' => $where,
            'whereBetween' => $whereBetween
        ];
    }

    /**
     * @param $params
     * @return array
     */
    public function getList ($params)
    {
        $filter = $this->getFilter($params);
        $list = $this->getListItems(MoneyLogModel::query(), $filter, $params['page_size'] ?? 20, [
            'id',
            'money',
            'type',
            'published_at',
            'created_at',
        ], 'page', [
            'published_at' => 'desc',
            'id' => 'desc'
        ]);
        return $list;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function save ($params)
    {
        $totalMoney = MoneyLogModel::orderBy('id', 'desc')->value('total');
        $params['total'] = $totalMoney * 100 + $params['money'];
        return @$this->saveOrUpdate('MoneyModel', $params);
    }

    public function getBalance ($format = false, $withBankData = false) {
        $user = request()->user();
        $sum = MoneyLogModel::query()->where('admin_id', $user['id'])->sum('money');
        $bankIn = MoneyLogModel::where('type', MoneyLogModel::BANK_IN)->sum('money');
        $bankOut = MoneyLogModel::where('type', MoneyLogModel::BANK_OUT)->sum('money');
        if ($withBankData) {
            return [
                'balance' => number_format($sum / 100, 2),
                'bankIn' => number_format($bankIn / 100, 2),
                'bankOut' => number_format($bankOut / 100, 2)
            ];
        } else {
            if ($format) {
                return number_format($sum / 100, 2);
            } else {
                return $sum;
            }
        }
    }

    public function addByOperateFinished ($money, $userId, $operateId)
    {
        $date = date('Y-m-d H:i:s');
        $type = MoneyLogModel::STOCK_EARN;
        if ($money < 0) {
            $type = MoneyLogModel::STOCK_LOST;
        }
        $operateRepository = new OperateRepository();
        $totalMoney = $operateRepository->getTotalMoney($operateId, $userId);
        $saveData = [
            'admin_id' => $userId,
            'money' => $money * 100,
            'total' => ($totalMoney + $money) * 100,
            'type' => $type,
            'published_at' => $date,
            'created_at' => $date,
            'updated_at' => $date,
        ];
        return MoneyLogModel::insert($saveData);
    }

    public function getUnfinishedMoney ()
    {
        $operateRepository = new OperateRepository();
        $operateList = OperateModel::query()->where('is_finished', OperateModel::UNFINISHED)->get();
        $listData = $operateRepository->addUnFinishDealData($operateList);
        return $this->getUnfinishedSum($listData['list']);
    }

    private function getUnfinishedSum ($list)
    {
        $sum = 0;
        foreach ($list as $item) {
            $sum += $item['stock_earnings'] * 100;
        }
        return $sum;
    }
}
